package com.tensquare.frined.controller;

import com.tensquare.frined.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/friend")
public class FriendController {
    @Autowired
    private FriendService friendService;
    @Autowired
    private HttpServletRequest request;


    @RequestMapping(value = "/like/{friendid}/{type}" , method = RequestMethod.PUT)
    public Result addFriend(@PathVariable String friendid , @PathVariable String type){
        //验证当前用户是否有userjuese
        Claims claims = (Claims) request.getAttribute("user_claims");
        if(claims == null){
            return new Result(false , StatusCode.LOGINERROR , "请先登录");
        }
        //获取当前用户的id
        String userid = claims.getId();
        //判断当前操作是添加好友还是添加非好友
        if(type.equals("1")){
            //添加好友
            friendService.addFriend(userid , friendid);
        }else {
            //添加非好友
            friendService.addNoFriend(userid ,friendid);
        }
        return new Result(true , StatusCode.OK , "添加好友成功");
    }
    @RequestMapping(value = "/{friendid}" , method = RequestMethod.DELETE)
    public Result deleteFriend(@PathVariable String friendid){
        //验证当前用户是否有userjuese
        Claims claims = (Claims) request.getAttribute("user_claims");
        if(claims == null){
            return new Result(false , StatusCode.LOGINERROR , "请先登录");
        }
        //获取当前用户的id
        String userid = claims.getId();
        //删除好友
        friendService.deleteFriend(userid ,friendid);
        return new Result(true , StatusCode.OK , "删除成功");
    }

}
